<?php declare(strict_types=1);

function task1()
{
    for ($i = 0; $i < 10; $i++) {
        echo 'task1->' . $i . "\n";
        yield;
    }
}

function task2()
{
    for ($i = 0; $i < 10; $i++) {
        echo 'task2->' . $i . "\n";
        yield;
    }
}

$list = [];
array_push($list, task1());
array_push($list, task2());
while (!empty($list)) {
    $coroutine = array_shift($list);
    $coroutine->send(null);
    // coroutine是否结束
    if ($coroutine->valid()) {
        array_push($list, $coroutine);
    }
}
